package com.steven.im.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;
import com.steven.im.R;
import com.steven.im.adapter.IMGroupListAdapter;
import com.steven.im.adapter.IMListViewAdapter;
import com.steven.im.base.BaseActivity;
import com.steven.im.entity.IMContacts;
import com.steven.im.utils.TitleBuilder;
import com.steven.im.widget.QuickIndexBar;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

public class IMGroupActivity extends BaseActivity {
    @BindView(R.id.quickIndexBar)
    QuickIndexBar quickIndexBar;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.currentWord)
    TextView currentWord;
    @BindView(R.id.title_left)
    ImageView titleLeft;
    @BindView(R.id.title_mid)
    TextView titleMid;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    private boolean isFirst = true;//默认第一次加载
    private ArrayList<IMContacts> friends = new ArrayList<>();
    private boolean isScale = false;
    private Handler handler = new Handler();

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_im_togroup;
    }

    @Override
    public void initView() {
        ivTitleRight.setVisibility(View.GONE);
        tvTitleRight.setTextColor(getResources().getColor(R.color.mainBg));
        new TitleBuilder(this).setTitleText(getString(R.string.friendsList))
                .setRightText(getString(R.string.next))
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public void initData() {
        loadData();
    }

    private void loadData() {
// 虚拟数据
        friends.add(new IMContacts("李伟"));
        friends.add(new IMContacts("张三"));
        friends.add(new IMContacts("阿三"));
        friends.add(new IMContacts("阿四"));
        friends.add(new IMContacts("段誉"));
        friends.add(new IMContacts("段正淳"));
        friends.add(new IMContacts("张三丰"));
        friends.add(new IMContacts("陈坤"));
        friends.add(new IMContacts("林俊杰1"));
        friends.add(new IMContacts("陈坤2"));
        friends.add(new IMContacts("王二a"));
        friends.add(new IMContacts("林俊杰a"));
        friends.add(new IMContacts("张四"));
        friends.add(new IMContacts("林俊杰"));
        friends.add(new IMContacts("王二"));
        friends.add(new IMContacts("王二b"));
        friends.add(new IMContacts("赵四"));
        friends.add(new IMContacts("杨坤"));
        friends.add(new IMContacts("赵子龙"));
        friends.add(new IMContacts("杨坤1"));
        friends.add(new IMContacts("李伟1"));
        friends.add(new IMContacts("宋江"));
        friends.add(new IMContacts("宋江1"));
        friends.add(new IMContacts("李伟3"));
        //2.对数据进行排序
        Collections.sort(friends);
        //3.设置Adapter
        listview.setAdapter(new IMGroupListAdapter(this, friends));
        quickIndexBar.setOnTouchLetterListener(new QuickIndexBar.OnTouchLetterListener() {
            @Override
            public void onTouchLetter(String letter) {
                //根据当前触摸的字母，去集合中找那个item的首字母和letter一样，然后将对应的item放到屏幕顶端
                for (int i = 0; i < friends.size(); i++) {
                    String firstWord = friends.get(i).getPinyin().charAt(0) + "";
                    if (!Character.isLetter(friends.get(i).getPinyin().charAt(0))) {
                        firstWord = "#";
                    }
                    if (letter.equals(firstWord)) {
                        //说明找到了，那么应该讲当前的item放到屏幕顶端
                        listview.setSelection(i);
                        break;//只需要找到第一个就行
                    } else {
                        listview.setSelection(i);
                    }
                }

                //显示当前触摸的字母
                showCurrentWord(letter);
            }
        });

        //通过缩小currentWord来隐藏
        ViewHelper.setScaleX(currentWord, 0);
        ViewHelper.setScaleY(currentWord, 0);
    }

    protected void showCurrentWord(String letter) {
        currentWord.setText(letter);
        if (!isScale) {
            isScale = true;
            ViewPropertyAnimator.animate(currentWord).scaleX(1f)
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(0).start();
            ViewPropertyAnimator.animate(currentWord).scaleY(1f)
                    .setInterpolator(new OvershootInterpolator())
                    .setDuration(0).start();
        }

        //先移除之前的任务
        handler.removeCallbacksAndMessages(null);

        //延时隐藏currentWord
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//				currentWord.setVisibility(View.INVISIBLE);
                ViewPropertyAnimator.animate(currentWord).scaleX(0f).setDuration(0).start();
                ViewPropertyAnimator.animate(currentWord).scaleY(0f).setDuration(0).start();
                isScale = false;
            }
        }, 500);
    }
}
